import { createRouter, createWebHistory } from 'vue-router'
import Home from '@/views/Home.vue'
import Login from '@/views/Login.vue'
import Register from '@/views/Register.vue'

// 工作日志相关页面
import WorkLog from '@/views/WorkLog.vue'
import WorkLogDetail from '@/views/WorkLogDetail.vue'

// 服务项目页面
import ClientVisit from '@/views/services/ClientVisit.vue'
import ClientVisitDetail from '@/views/services/ClientVisitDetail.vue'
import IntegrityBuilding from '@/views/services/IntegrityBuilding.vue'
import IntegrityBuildingDetail from '@/views/services/IntegrityBuildingDetail.vue'
import ClientEvaluation from '@/views/services/ClientEvaluation.vue'
import ClientEvaluationDetail from '@/views/services/ClientEvaluationDetail.vue'
import SatisfactionSurvey from '@/views/services/SatisfactionSurvey.vue'
import SatisfactionSurveyDetail from '@/views/services/SatisfactionSurveyDetail.vue'
import TeamCollaboration from '@/views/services/TeamCollaboration.vue'
import TeamCollaborationDetail from '@/views/services/TeamCollaborationDetail.vue'

// 任务提醒页面
import TaskReminder from '@/views/TaskReminder.vue'

// 客户管理页面
import ClientManagement from '@/views/ClientManagement.vue'
import ClientEdit from '@/views/ClientEdit.vue'

// 通用服务页面
import GenericService from '@/views/services/GenericService.vue'

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    meta: {
      title: '客户经理工作台',
      showTabbar: true,
      requiresAuth: true
    }
  },
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: {
      title: '客户经理登录',
      showTabbar: false
    }
  },
  {
    path: '/register',
    name: 'Register',
    component: Register,
    meta: {
      title: '注册',
      showTabbar: false
    }
  },

  // 工作日志
  {
    path: '/work-log',
    name: 'WorkLog',
    component: WorkLog,
    meta: {
      title: '工作日志',
      showTabbar: true,
      requiresAuth: true
    }
  },
  {
    path: '/work-log/:date',
    name: 'WorkLogDetail',
    component: WorkLogDetail,
    meta: {
      title: '工作日志详情',
      showTabbar: false,
      requiresAuth: true
    }
  },
  // 客户拜访
  {
    path: '/client-visit',
    name: 'ClientVisit',
    component: ClientVisit,
    meta: {
      title: '客户拜访',
      showTabbar: true,
      requiresAuth: true
    }
  },
  {
    path: '/client-visit/detail',
    name: 'ClientVisitDetail',
    component: ClientVisitDetail,
    meta: {
      title: '客户拜访详情',
      showTabbar: false,
      requiresAuth: true
    }
  },
  // 诚信五化建设
  {
    path: '/integrity-building',
    name: 'IntegrityBuilding',
    component: IntegrityBuilding,
    meta: {
      title: '诚信五化建设',
      showTabbar: true,
      requiresAuth: true
    }
  },
  {
    path: '/integrity-building/detail',
    name: 'IntegrityBuildingDetail',
    component: IntegrityBuildingDetail,
    meta: {
      title: '诚信五化建设详情',
      showTabbar: false,
      requiresAuth: true
    }
  },
  // 客户评价
  {
    path: '/client-evaluation',
    name: 'ClientEvaluation',
    component: ClientEvaluation,
    meta: {
      title: '客户评价',
      showTabbar: true,
      requiresAuth: true
    }
  },
  {
    path: '/client-evaluation/detail',
    name: 'ClientEvaluationDetail',
    component: ClientEvaluationDetail,
    meta: {
      title: '客户评价详情',
      showTabbar: false,
      requiresAuth: true
    }
  },
  // 满意度调查
  {
    path: '/satisfaction-survey',
    name: 'SatisfactionSurvey',
    component: SatisfactionSurvey,
    meta: {
      title: '满意度调查',
      showTabbar: true,
      requiresAuth: true
    }
  },
  {
    path: '/satisfaction-survey/detail',
    name: 'SatisfactionSurveyDetail',
    component: SatisfactionSurveyDetail,
    meta: {
      title: '满意度调查详情',
      showTabbar: false,
      requiresAuth: true
    }
  },
  // 四员协同
  {
    path: '/team-collaboration',
    name: 'TeamCollaboration',
    component: TeamCollaboration,
    meta: {
      title: '四员协同',
      showTabbar: true,
      requiresAuth: true
    }
  },
  {
    path: '/team-collaboration/detail',
    name: 'TeamCollaborationDetail',
    component: TeamCollaborationDetail,
    meta: {
      title: '四员协同详情',
      showTabbar: false,
      requiresAuth: true
    }
  },
  // 任务提醒
  {
    path: '/task-reminder',
    name: 'TaskReminder',
    component: TaskReminder,
    meta: {
      title: '任务提醒',
      showTabbar: false,
      requiresAuth: true
    }
  },
  // 客户管理
  {
    path: '/client-management',
    name: 'ClientManagement',
    component: ClientManagement,
    meta: {
      title: '客户管理',
      showTabbar: true,
      requiresAuth: true
    }
  },
  // 客户信息编辑
  {
    path: '/client/edit/:clientId',
    name: 'ClientEdit',
    component: ClientEdit,
    meta: {
      title: '编辑客户信息',
      showTabbar: false,
      requiresAuth: true
    }
  },
  // 通用服务页面（用于处理动态添加的服务项目）
  {
    path: '/service/:serviceName',
    name: 'GenericService',
    component: GenericService,
    meta: {
      title: '服务项目',
      showTabbar: true,
      requiresAuth: true
    }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, _from, next) => {
  // 设置页面标题
  if (to.meta?.title) {
    let title = to.meta.title as string
    // 对于通用服务页面，使用服务名称作为标题
    if (to.name === 'GenericService' && to.params.serviceName) {
      title = decodeURIComponent(to.params.serviceName as string)
    }
    document.title = title
  }

  // 检查是否需要认证
  if (to.meta?.requiresAuth) {
    const token = localStorage.getItem('auth_token') || localStorage.getItem('manager_token')
    if (!token) {
      // 未登录，跳转到登录页
      next('/login')
      return
    }
  }

  // 如果已登录且访问登录页，跳转到首页
  if (to.path === '/login') {
    const token = localStorage.getItem('manager_token')
    if (token) {
      next('/')
      return
    }
  }

  next()
})

export default router
